package com.javaweb.dao.impl;

import com.javaweb.aop.TimeMeasure;
import com.javaweb.common.StringUtils;
import com.javaweb.dao.UserDao;
import com.javaweb.model.User;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.sql.Date;
import java.util.List;

/**
 * @desc:
 * @author: Ping
 * @date: 2024/9/23 16:10
 */
@Repository
@Slf4j
public class UserDaoImpl implements UserDao {

    @Autowired
    private JdbcTemplate jdbcTemplate ;
    @Override
    public Integer insert(User user) {

        /**
         * SQL 逻辑
         */
        String sql = "INSERT INTO tb_user (name, password, salt, create_time, gender) VALUES" +
                "(?, ?, ?, ? ,?)" ;
        Object[] params = new Object[] {
                user.getName(),
                StringUtils.md5Encoder(user.getPassword(), user.getSalt()),
                user.getSalt(),
                new Date(user.getCreateTime().getTime()),
                user.getGender() } ;
        return jdbcTemplate.update(sql, params) ;
    }

    @Override
    public Integer insertUserRole(Integer userId, Integer roleId) {
        String sql = "INSERT INTO tb_user_role (user_id, role_id) VALUES (?,?)" ;
        Object[] params = new Object[] {
                userId,
                roleId
        } ;
       return jdbcTemplate.update(sql, params) ;

    }

    @Override
    public User selectOne(String name, String password) {
        String sql = "SELECT tu.* " +
                " FROM tb_user tu " +
                " WHERE tu.name=? and tu.password=?" ;
        Object[] params = new Object[] {
                name,
                StringUtils.md5Encoder(password, StringUtils.getSalt(name))
        } ;
        return jdbcTemplate.query(sql, params, rs -> {
            if (rs.next()) {
                User user = new User()
                        .setUserId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setPassword(rs.getString(3))
                        .setSalt(rs.getString(4))
                        .setCreateTime(rs.getDate(5))
                        .setGender(rs.getInt(6))
                        .setScore(rs.getInt(7))
                        .setStatus(rs.getInt(8))
                        .setEmail(rs.getString(9)) ;
                return user ;
            }
            return null;
        });
    }

    @Override
    public User selectOneById(Integer userId) {
        String sql = "SELECT * FROM tb_user WHERE user_id=?" ;
        return jdbcTemplate.query(sql, new Object[]{userId}, rs -> {
            if (rs.next()) {
                User user = new User()
                        .setUserId(rs.getInt(1))
                        .setName(rs.getString(2))
                        .setPassword(rs.getString(3))
                        .setSalt(rs.getString(4))
                        .setCreateTime(rs.getDate(5))
                        .setGender(rs.getInt(6))
                        .setScore(rs.getInt(7))
                        .setStatus(rs.getInt(8))
                        .setEmail(rs.getString(9)) ;
                return user ;
            }
            return null;
        });
    }

    @Override
    public List<User> selectByName(String name) {

        return null;
    }

    @Override
    public List<User> selectByRole(String roleName) {
        String sql = "SELECT u.* FROM tb_role r " +
                                 " JOIN tb_user_role ur ON r.role_id = ur.role_id" +
                                 " JOIN tb_user u ON u.user_id = ur.user_id" +
                                "  WHERE r.name = ?" ;


       return null ;
    }

    @TimeMeasure
    @Override
    public Integer selectCountAll() {
        return null ;
    }

    @Override
    public Integer updateScore(Integer userId, int increased) {
        String sql = "UPDATE tb_user SET score = score + ? WHERE user_id = ?" ;
//        try (
//                PreparedStatement ps = this.conn.prepareStatement(sql) ;) {
//            ps.setInt(1, increased) ;
//            ps.setInt(2, userId) ;
//            return ps.executeUpdate() ;
//        }catch (SQLException e) {
//            log.error("execute update user score: " + e.getMessage()) ;
//        }
        return jdbcTemplate.update(sql, new Object[]{increased, userId});
    }
}
